package com.yuelife.market.controller;

import com.yuelife.market.entity.MAddress;
import com.yuelife.market.service.MAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "AddressController")
@RestController
@RequestMapping("/address")
@CrossOrigin//解决跨域的注解  但是只能局部  不能全局 全局需要在启动类中写个bean
public class AddressController {
    @Autowired
    private MAddressService mAddressService;

    /**
     * 初始化获得用户默认的收货信息（默认地址）
     * @param uid
     * @return
     */
    @ApiOperation(value = "根据用户id查询用户的默认收货地址")
    @ApiImplicitParam(paramType = "path", name = "uid", value = "用户id", required = true, dataType = "Integer")
    @GetMapping("/defaultSite/{ownerId}")
    public Map defaultSite(@PathVariable("ownerId") Integer uid){
        Map map=new HashMap();
        try {
            MAddress mAddress=mAddressService.queryById(uid);
            map.put("data",mAddress);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data",null);
            map.put("code",500);
            return map;
        }
    }

    /**
     * 根据用户id查询该用户所有的收货地址信息，是多个信息，
     * 其中字段siteDefault  是否默认地址  1默认，0不是
     * @param uid
     * @return
     */
    @ApiOperation(value = "根据用户id查询用户的所有收货地址")
    @ApiImplicitParam(paramType = "path", name = "uid", value = "用户id", required = true, dataType = "Integer")
    @GetMapping("/userAllAddress/{ownerId}")
    public Map userAllAddress(@PathVariable("ownerId") Integer uid){
        Map map=new HashMap();
        try {
            List<MAddress> mAddressList=mAddressService.selectAllAddressByUid(uid);
            map.put("data",mAddressList);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data",null);
            map.put("code",500);
            return map;
        }
    }

    /**
     * 删除收货信息
     * 根据收货信息id删除对应的收货信息
     * @return
     */
    @ApiOperation(value = "根据地址id删除该收获地址")
    @ApiImplicitParam(paramType = "path", name = "aid", value = "地址id", required = true, dataType = "Integer")
    @DeleteMapping("/deleteAddress/{addressId}")
    public Map deleteAddress(@PathVariable("addressId") Integer aid){
        Map map=new HashMap();
        try {
            mAddressService.deleteById(aid);
            map.put("message","删除成功！");
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
            map.put("code",500);
            return map;
        }
    }

    /**
     * 在编辑收货地址的时候，先需要进行查询
     * 因为参数是对象，所以可以根据条件信息查询
     * @param mAddress
     * @return
     */
    @ApiOperation(value = "查询收获地址详情")
    @ApiImplicitParam(name = "mAddress", value = "mAddress地址对象", required = true, dataType = "MAddress")
    @GetMapping("/selectAddressByaid")
    public Map selectAddressByaid(MAddress mAddress){
        System.out.println("我是");
        Map map=new HashMap();
        try {
            MAddress ma=mAddressService.selectByAid(mAddress);
            map.put("data",ma);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data",null);
            map.put("code",500);
            return map;
        }
    }


    /**
     * 修改地址，以及默认地址
     * 如果有修改为默认地址，那么就需要将其他所有的地址先修改为不是默认的，然后这个勾选默认的地址修改了
     * 所以在业务中处理两个操作，先判断有没有修改为默认地址，如果修改了，需要修改所有的地址为不是默认状态的地址，然后再修改此次操作的地址
     * 由于同时涉及了数据库的两个操作，所以加事务注解 @Transcational  不过是在业务层处理
     * @return
     */
    @ApiOperation(value = "编辑收货地址")
    @ApiImplicitParam(name = "mAddress", value = "mAddress地址对象", required = true, dataType = "MAddress")
    @PutMapping("/updataAddress")
    public int updataAddress(MAddress mAddress){
        System.out.println("用户id"+mAddress.getUid());
        System.out.println("状态"+mAddress.getSiteDefault());
        System.out.println(mAddress.getAid());
        System.out.println(""+mAddress.getSiteName());
        System.out.println(""+mAddress.getSiteDetails());
        System.out.println(mAddress.getSiteNote());
        try {
            mAddressService.update(mAddress);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }

    /**
     * 增加收货地址
     * @param mAddress
     * @return
     */
    @ApiOperation(value = "增加收货地址")
    @ApiImplicitParam(name = "mAddress", value = "mAddress地址对象", required = true, dataType = "MAddress")
    @PostMapping("/addAddress")
    public int addAddress(MAddress mAddress){
        try {
            mAddressService.insert(mAddress);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
            return 500;
        }
    }







}
